package com.zretc.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.zretc.entity.Attention;
import com.zretc.service.AttentionService;
import com.zretc.service.impl.AttentionServiceImpl;
import com.zretc.util.PageInfo;

/**
 * Servlet implementation class AttentionServlet
 */
@WebServlet("/AttentionServlet")
@MultipartConfig
public class AttentionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private AttentionService attentionService = new AttentionServiceImpl();
    Gson gson = new Gson();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AttentionServlet() {
        super();
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if ("existAttention".equals(op)) {
			doExistAttention(request,response);
		}else if ("addAttention".equals(op)) {
			doAddAttention(request,response);
		}else if ("deleteAttention".equals(op)) {
			doDeleteAttention(request,response);
		}else if ("getAttentionSinger".equals(op)) {
			doGetAttentionSinger(request,response);
		}
	}
	
	//获取该用户关注的歌手用以做渲染
	private void doGetAttentionSinger(HttpServletRequest request, HttpServletResponse response) throws IOException {
		Map<String, String> map = new HashMap<String, String>();
		String accountId = request.getParameter("accountId");
		String pageNum = request.getParameter("pageNum");
		String pageSize = request.getParameter("pageSize");
		map.put("accountId", accountId);
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		PageInfo<Attention> attentionByAccountId = attentionService.getAttentionByAccountId(map);
		String json = gson.toJson(attentionByAccountId);
		response.getWriter().write(json);
	}

	//取消关注
	private void doDeleteAttention(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String singerId = request.getParameter("singerId");
		String accountId = request.getParameter("accountId");
		int deleteAttention = attentionService.deleteAttentionByAccountIdAndSingerId(Integer.parseInt(accountId), Integer.parseInt(singerId));
		if (deleteAttention>0) {
			response.getWriter().write("deleteYes");
		}else {
			response.getWriter().write("deleteNo");
		}
	}

	//添加关注
	private void doAddAttention(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String singerId = request.getParameter("singerId");
		String accountId = request.getParameter("accountId");
		int addAttention = attentionService.addAttention(Integer.parseInt(accountId), Integer.parseInt(singerId));
		if (addAttention>0) {
			response.getWriter().write("addYes");
		}else {
			response.getWriter().write("addNo");
		}
	}

	//判断是否已关注
	private void doExistAttention(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String singerId = request.getParameter("singerId");
		String accountId = request.getParameter("accountId");
		Attention attentionByAccountIdAndSingerId = attentionService.getAttentionByAccountIdAndSingerId(Integer.parseInt(accountId), Integer.parseInt(singerId));
		if (attentionByAccountIdAndSingerId==null) {
			response.getWriter().write("noAttention");
		}else if (attentionByAccountIdAndSingerId!=null) {
			response.getWriter().write("yesAttention");
		}
	}

}
